<?php
namespace app\index\service;


use app\index\model\DeviceJieModel;
use app\index\model\DeviceJinModel;
use app\index\model\DeviceLinModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginSpeedModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongModel;
use app\index\model\VisaModel;

class SceneService extends CommonService
{

    public function check_engin_diary_dt($dt, $enginid, $id=0){
        return !!EnginDiaryModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'dt'=>$dt,'deleted_user'=>0])->where('id','neq', $id)->find();
    }


    public function get_engin_xun_level(){
        return [
            '一般问题',
            '严重问题',
            '重大问题',
        ];
    }

    public function check_speed_month_name($name, $month, $enginid, $id=0){
        return !!SpeedMonthModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'month'=>$month,'name'=>$name,'deleted_user'=>0])
            ->where('id','neq', $id)->find();
    }

    public function check_engin_speed_month($month, $enginid, $id=0){
        return !!EnginSpeedModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'month'=>$month,'deleted_user'=>0])
            ->where(['isturn'=>1])->where('status','in',[0,1])->where('id','neq', $id)->find();
    }

    public function check_engin_chan_month($month, $enginid, $id=0){
        return !!EnginChanModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'month'=>$month,'deleted_user'=>0])
            ->where(['isturn'=>1])->where('status','in',[0,1])->where('id','neq', $id)->find();
    }



    public function create_speed_zong_number(){
        $tou = 'ZJDJH';
        $daystr = substr(date("Ymd"), 2);
        $number = SpeedZongModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_speed_month_number(){
        $tou = 'YDJH';
        $daystr = substr(date("Ymd"), 2);
        $number = SpeedMonthModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_engin_speed_number(){
        $tou = 'JDTB';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginSpeedModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_engin_chan_number(){
        $tou = 'CZTB';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginSpeedModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }



    public function create_visa_number(){
        $tou = 'QZD';
        $daystr = substr(date("Ymd"), 2);
        $number = VisaModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }



    public function create_engin_xun_number(){
        $tou = 'XJBG';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginXunModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_engin_zheng_number(){
        $tou = 'ZGBG';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginZhengModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }



    //获取 项目 资料 的 文件夹（所有层级）
    public function get_engin_fold_all($pid=0, $enginid){
        $data = EnginFoldModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'pid'=>$pid,'type'=>0, 'enginid'=>$enginid])->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_engin_fold_all($item['id'],$enginid);
        }
        return $data;
    }



    public function get_zheng_gaista(){
        return [
            0 => '待整改',
            1 => '已整改',
        ];
    }
    public function get_zheng_gaista_name($gaista){
        $arr = $this->get_zheng_gaista();
        $str = isset($arr[$gaista]) ? $arr[$gaista] : '';
        return $str;
    }


    public function get_zheng_fusta(){
        return [
            1 => '待复检',
            2 => '复检通过',
            3 => '复检不通过',
        ];
    }
    public function get_zheng_fusta_name($fusta){
        $arr = $this->get_zheng_fusta();
        $str = isset($arr[$fusta]) ? $arr[$fusta] : '';
        return $str;
    }


    public function get_fold_filenums($id){
        $num = 0;
        $arr = [$id];

        while (count($arr) > 0) {
            foreach ($arr as $fk=>$foldid){
                $info = EnginFoldModel::where(['pid'=>$foldid])->select()->toArray();
                unset($arr[$fk]);
                foreach ($info as $vv){
                    if($vv['type'] == 1){
                        $num += 1;
                    }else{
                        $arr[] = $vv['id'];
                    }
                }
            }
        }
        return $num;
    }


}
